package com.superyee.commonlib.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.superyee.commonlib.R;

import java.io.File;


/**
 * Created by guoguo on 2017/11/18.
 */

public class LoadImageUitl {




    /**
     * 加载图片
     *
     * @param url
     * @param view
     */
    public static void loadImage(String url, ImageView view) {
        loadImage(url, view, R.drawable.comm_image_default);
    }

    /**
     * 加载图片
     *
     * @param url
     * @param view
     * @param placeHolder 加载过程中占位图
     */
    public static void loadImageFade(String url, ImageView view, int placeHolder) {
        RequestOptions options = new RequestOptions();
        options.placeholder(placeHolder).error(placeHolder);
        Glide.with(view).load(url)
                .apply(options).transition(new DrawableTransitionOptions().crossFade(200))
                .into(view);
    }

    /**
     * 加载图片
     *
     * @param url
     * @param view
     */
    public static void loadImageFade(String url, ImageView view) {
        Glide.with(view).load(url)
                .transition(new DrawableTransitionOptions().crossFade(200))
                .into(view);
    }

    /**
     * 加载图片
     *
     * @param url
     * @param view
     * @param placeHolder 加载过程中占位图
     */
    public static void loadImage(String url, ImageView view, int placeHolder) {
        RequestOptions options = new RequestOptions();
        options.placeholder(placeHolder).error(placeHolder).fallback(placeHolder);
        Glide.with(view).load(url)
                .apply(options)
                .into(view);
    }


    public static void loadImgeNoHolder(String url, ImageView imageView) {
        Glide.with(imageView).load(url)
                .into(imageView);
    }

    public static void loadImage(File file, ImageView image) {
        Glide.with(image).load(file).into(image);
    }

    /**
     * 加载圆形图片
     *
     * @param url
     * @param imageView
     */
    public static void loadImageCircle(String url, ImageView imageView) {
//        Glide.with(imageView).load(url).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(imageView);
        Glide.with(imageView).load(url).apply(RequestOptions.circleCropTransform()).into(imageView);
    }

    public static void downloadBitmap(Context context, String url, final OnDownload onDownload) {
        Glide.with(context).asBitmap().load(url).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(final Bitmap resource, Transition<? super Bitmap> transition) {
                if (onDownload != null) {
                    onDownload.onDownloadSucceed(resource);
                }
            }

            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {
                if (onDownload != null) {
                    onDownload.onDownloadFailed();
                }
            }
        });
    }

    public static void downloadDrawable(Context context, String url, final OnDownload onDownload) {
        Glide.with(context).asDrawable().load(url).into(new SimpleTarget<Drawable>() {

            @Override
            public void onResourceReady(final Drawable drawable, Transition<? super Drawable> transition) {
                if (onDownload != null) {
                    onDownload.onDownloadSucceed(drawable);
                }
            }

            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {
                if (onDownload != null) {
                    onDownload.onDownloadFailed();
                }
            }
        });
    }

    public interface OnDownload<T> {
        void onDownloadSucceed(T t);

        void onDownloadFailed();
    }
}
